package com.mb.app.controller;

import com.mb.app.domain.MpSmsLog;
import com.mb.app.service.IMpSmsLogService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 短信记录  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-20
 */
@Controller
@RequestMapping("/app/mpSms")
@Slf4j
public class MpSmsLogController extends BaseController {

    @Autowired
    private IMpSmsLogService mpSmsLogService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/index")
    String index(HttpServletRequest request){
        // 剩余次数
        int total = mpSmsLogService.counts();
        // 设置总次数
        int times = (int) redisUtil.get("smsTotalCount");
        int remainingTimes = times - total;
        request.setAttribute("remainingTimes",remainingTimes);
        return "/app/mpSms/mpSms_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<MpSmsLog> list = mpSmsLogService.queryList(query);
        int total = mpSmsLogService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @ResponseBody
    @GetMapping("/getRemainingTimes")
    public int getRemainingTimes() {
        // 剩余次数
        int total = mpSmsLogService.counts();
        // 设置总次数
        int times = (int) redisUtil.get("smsTotalCount");
        int remainingTimes = times - total;
        return remainingTimes;
    }
}
